home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11465 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  73 lines

  1. Path: kbad.eglin.af.mil!rpi!not-for-mail
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.lang.c++,comp.lang.c++.moderated
  4. Subject: Re: Enumerated type converted to pointer? Legal?
  5. Followup-To: comp.lang.c++,comp.lang.c++.moderated
  6. Date: 14 Mar 1996 17:56:34 -0000
  7. Organization: GABI Software, Sarl.
  8. Sender: cppmods@netlab.cs.rpi.edu
  9. Approved: rmartin@oma.com
  10. Message-ID: <4i9mki$ge5@netlab.cs.rpi.edu>
  11. References: <4hobji$mco@netlab.cs.rpi.edu> <4i703u$1gl@netlab.cs.rpi.edu>
  12. NNTP-Posting-Host: netlab.cs.rpi.edu
  13. X-Original-Date: 14 Mar 1996 11:58:25 GMT
  14.  
  15. In article <4i703u$1gl@netlab.cs.rpi.edu> shappir@libra.math.tau.ac.il
  16. (Shappir Dan) writes:
  17.  
  18. |> { comp.std.c++ removed from followups. -mod}
  19.  
  20. |> Brad Stowers (bstowers@pobox.com) wrote:
  21.  
  22. |> : Compile and run the above program.  Is it just me, or should the last line
  23. |> : of the main() function ( FooBar(x, mdType1); ) have caused a type mismatch
  24. |> : error?  I think I know why it didn't:  mdType1 evaluates to an integer with
  25. |> : a value of 0, which is a valid value to pass as a pointer.  In support of
  26. |> : this theory, the line:
  27.  
  28. |> This is due to one of the differences between C and C++. In C the NULL
  29. |> pointer is defined as ((void*)0) while in C++ it's simply 0.
  30.  
  31. This is false.  In both C and C++, the exact definition of NULL depends
  32. on the implementation.  On most Unix systems, for example, both are
  33. defined as 0, but I have worked on systems (before C++) where NULL was
  34. 0L.
  35.  
  36. The only difference between C and C++ in this regard is that ((void*)0)
  37. is legal in C, but not in C++.  The only reason that an implementor
  38. would maintain different definitions is historical: he used ((void*)0)
  39. in C before implementing C++, and is afraid that changing it would break
  40. customer code.  (In fact, any code it broke was already broken, but it
  41. is often easier for the implementor to support the broken code that to
  42. explain to the customer why it worked in one release and not in the
  43. next.)
  44.  
  45. |> This can raise
  46. |> all sorts of type conversion problems, for example:
  47.  
  48. |> void foo(int bar) { ... }
  49. |> void foo(void* bar) { ... }
  50.  
  51. |> ...
  52. |> foo(0);
  53. |> ...
  54.  
  55. |> The compiler will not be able to determine which version of foo to call.
  56.  
  57. The compiler had better call `foo( int )', which is an exact match.
  58. More surprising, since NULL is generally defined as 0, `foo( NULL )'
  59. also calls `foo( int )'.  (Fergus Henderson once modified g++ to have a
  60. real null pointer, which would only match pointer types, but his
  61. modifications are not part of the standard, and as far as I know, have
  62. never been incorporated in a real g++ release.)
  63. -- 
  64. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  65. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  66. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  67.               -- A la recherche d'une activitΘ dans une region francophone
  68.  
  69.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  70.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  71.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  72.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  73.